Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

[FRAME] Simple multi block migrations #14275

Draft
wants to merge 66 commits into
base: master
Choose a base branch
from
Draft

Conversation

ggwpez
Copy link
Member

@ggwpez ggwpez commented May 31, 2023

Partial paritytech/polkadot-sdk#198 (on_poll in #14279).

pallet-migrations

This pallet takes a configuration of migrations Vec<Box<dyn SteppedMigration<…>>> and executes these in order on_initialize. The execution starts on each runtime upgrade. The unique identifiers of each executed migration are stored to prevent accidental re-execution of old ones.
While migrations are executed, only Mandatory extrinsics and inherents are allowed to execute. This is accomplished by implementing MultiStepMigrator for the pallet and querying it in frame-executive before each non-mandatory TX. Concretely this means that these transactions will panic and make a block un-importable. The BlockBuilder respects this limitation and retains all extrinsics in the pool while migrations are ongoing. Once the MBMs are done, these extrinsics will start to be included again. From a user's perspective this just results in an increased time until the TX is included.

The pallet supports an UpgradeStatusHandler that can be used to notify external logic of upgrade start/finish (for example to pause XCM dispatch).

Error recovery is very limited in the case that a migration errors or times out (exceeds its max_steps). Currently the runtime dev can decide in UpgradeStatusHandler::failed how to handle this. One follow-up would be to pair this with the SafeMode pallet and enact safe mode when an upgrade fails, to allow governance to rescue the chain. This is currently not possible, since governance is not Mandatory.

frame-executive + BlockBuilder

Executive now additionally takes an MultiStepMigrator trait that defaults to ().
IT IS PARAMOUNT TO SET THIS TO THE MIGRATIONS PALLET WHEN YOU DEPLOY IT.
The kitchensink runtime contains an example.

The block builder is aware of the new after_inherents function and uses it to figure out the MBM state.
As comparison the old and new logic (changes with +):

Current order:
	on_runtime_upgrade
	System::initialize
	on_initialize
	apply inherents
	apply extrinsics
	on_idle
	on_finalize

New order:
	on_runtime_upgrade
	System::initialize
	on_initialize
	apply inherents
+	after_inherents:
+		progress mbms
+		poll TBD
+	If not MBM:
+		apply extrinsics
+		on_idle
	on_finalize

Runtime API

frame-support

  • SteppedMigration: A multi-step migration.
  • ExtrinsicSuspenderQuery: Implemented by the migrations pallet and used by frame-executive to check TX suspension.
  • UpgradeStatusHandler: React to upgrade start, completion and failure.
  • FailedUpgradeHandling: Decide how to handle a failed upgrade. Either ForceUnstuck or KeepStuck.

TODO

  • Consume weight correctly
  • Cleanup

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
frame/executive/src/lib.rs Outdated Show resolved Hide resolved
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
@ggwpez ggwpez marked this pull request as ready for review May 31, 2023 16:29
@ggwpez ggwpez requested review from a team and kianenigma May 31, 2023 16:29
@ggwpez ggwpez added the A3-in_progress Pull request is in progress. No review needed at this stage. label May 31, 2023
@ggwpez ggwpez self-assigned this May 31, 2023
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
@paritytech-ci paritytech-ci requested a review from a team May 31, 2023 17:24

/// A migration that can proceed in multiple steps.
pub trait SteppedMigration {
fn id(&self) -> BoundedVec<u8, ConstU32<32>>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vec is also fine here, but I have usually seen types like [8u; 32] being used for such cases.

Copy link
Member Author

@ggwpez ggwpez Jun 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also made it generic. But with the new config defaults, we could add some sane default like this 🤔

@paritytech-ci paritytech-ci requested a review from a team May 31, 2023 17:25
Copy link
Contributor

@kianenigma kianenigma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far everything LGTM.

Can fn poll() be done in a separate PR? I presume yes, and perhaps it would be good for someone to start that already.

One important topic related to https://github.com/paritytech/substrate/pull/14275/files#r1211972428 is also tx-pool validation. @ggwpez and I discussed if fn validate_transaction should also be aware of transaction suspension, and signal the pool that everything is "temporarily" invalid.

@liamaharon
Copy link
Contributor

liamaharon commented May 31, 2023

@kianenigma am I right to assume we'll want to make it easy to test these with try-runtime-cli? Ideally should be as easy as on-runtime-upgrade, maybe make it auto-mine blocks until all multi-block migrations are complete & provide a hook that runs afterwards?

@xlc
Copy link
Contributor

xlc commented May 31, 2023

Does this pauses user tx and incoming XCM etc? because otherwise it will be bad.

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
@ggwpez
Copy link
Member Author

ggwpez commented Jun 1, 2023

Does this pauses user tx and incoming XCM etc? because otherwise it will be bad.

It pauses non-mandatory extrinsics in frame-executive by checking a flag. For XCM we probably need a suspension and resume hook additionally.

But now that Im thinking about this again; this would make it impossible to rescue if one of the migrations errors, since governance requires things like the referenda pallet.
So maybe in the case that something errors, it could be put into SafeMode to re-allow few extrinsics.

@kianenigma
Copy link
Contributor

@kianenigma am I right to assume we'll want to make it easy to test these with try-runtime-cli? Ideally should be as easy as on-runtime-upgrade, maybe make it auto-mine blocks until all multi-block migrations are complete & provide a hook that runs afterwards?

Good to think about it early, and yes I think it is possible with little effort. I believe follow-chain will already do everything needed to test an MBM?

@liamaharon liamaharon self-requested a review June 15, 2023 17:03
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
@ggwpez ggwpez added A3-in_progress Pull request is in progress. No review needed at this stage. and removed A0-please_review Pull request needs code review. labels Jun 16, 2023
ggwpez and others added 10 commits June 16, 2023 14:08
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
@paritytech-cicd-pr
Copy link

The CI pipeline was cancelled due to failure one of the required jobs.
Job name: cargo-check-benches
Logs: https://gitlab.parity.io/parity/mirrors/substrate/-/jobs/3092555

@paritytech-cicd-pr
Copy link

The CI pipeline was cancelled due to failure one of the required jobs.
Job name: cargo-check-benches
Logs: https://gitlab.parity.io/parity/mirrors/substrate/-/jobs/3092556

@kianenigma
Copy link
Contributor

Is this more or less ready for final review?

@kianenigma kianenigma self-requested a review July 20, 2023 14:03
@ggwpez
Copy link
Member Author

ggwpez commented Jul 20, 2023

Still first need to merge #14414. I will ping you there for review when its ready.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A3-in_progress Pull request is in progress. No review needed at this stage. B1-note_worthy Changes should be noted in the release notes C1-low PR touches the given topic and has a low impact on builders. D9-needsaudit 👮 PR contains changes to fund-managing logic that should be properly reviewed and externally audited T1-runtime This PR/Issue is related to the topic “runtime”.
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

[FRAME Core] Simple Multi-block Migrations
8 participants